package com.university.sol.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.university.sol.common.Constant;
import com.university.sol.dao.TeamMapper;
import com.university.sol.pojo.Team;
import com.university.sol.service.TeamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author 侯瑞阳
 * @Date 2019/6/9 11:26
 **/
@Service
public class TeamServiceImpl implements TeamService {
    @Autowired
    private TeamMapper teamMapper;
    /**
     * 根据用户邮箱，标签分页查询团级信息
     * @param userEmail
     * @param tag
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public PageInfo<List> getByEmailAndTag(String userEmail, String tag, Integer pageNum, Integer pageSize) {
        if (tag.equals("")) {
            tag = null;
        }
        PageHelper.startPage(pageNum, pageSize);
        // 去数据库查询相关信息
        List<Team> list = teamMapper.getByEmailAndTag(userEmail, tag);
        List<Team> teams = new ArrayList();
        // 遍历集合
        for(Team team : list) {
            Team newTeam = new Team();
            newTeam.setTeamNumber(team.getTeamNumber());
            // 设置标题
            newTeam.setTeamTitle(team.getTeamTitle());
            // 设置点赞数
            newTeam.setTeamLikeNum(team.getTeamLikeNum());
            // 设置创建时间
            newTeam.setCreateTime(team.getCreateTime());
            // 设置图片
            newTeam.setTeamImgUrl(Constant.IMG_URL + headImg(team.getTeamImgUrl()));
            teams.add(newTeam);
        }
        PageInfo listPageInfo = new PageInfo<>(teams);
        return listPageInfo;
    }

    /**
     * 根据邮箱和团级编号查询详情
     * @param userEmail
     * @param teamNum
     * @return
     */
    @Override
    public Team getByEmailAndTeamNum(String userEmail, Long teamNum) {
        return teamMapper.getByEmailAndTeamNum(userEmail, teamNum);
    }

    /**
     * 获取图片集合
     * @param teamImgUrl
     * @return
     */
    @Override
    public List<String> getImgs(String teamImgUrl) {
        String[] strings = teamImgUrl.split(",");
        List<String> stringList = new ArrayList<>();
        for(String string : strings) {
            stringList.add(Constant.IMG_URL + string);
        }
        return stringList;
    }

    /**
     * 根据点赞数最多的获取
     * @param userEmail
     * @return
     */
    @Override
    public List<Team> getByLikeNum(String userEmail) {
        List<Team> teams = teamMapper.getByLikeNum(userEmail);
        List<Team> teamList = new ArrayList<>();
        for (Team team :teams) {
            Team newTeam = new Team();
            newTeam.setCreateTime(team.getCreateTime());
            newTeam.setTeamLikeNum(team.getTeamLikeNum());
            newTeam.setSimpleDesc(team.getSimpleDesc());
            newTeam.setTeamImgUrl(Constant.IMG_URL + headImg(team.getTeamImgUrl()));
            teamList.add(newTeam);
        }
        return teamList;
    }

    /**
     * 查询最近的团级记录
     * @param userEmail
     * @return
     */
    @Override
    public List<Team> getByRecent(String userEmail) {
        List<Team> teams = teamMapper.getByRecent(userEmail);
        List<Team> teamList = new ArrayList<>();
        for (Team team :teams) {
            Team newTeam = new Team();
            newTeam.setTeamNumber(team.getTeamNumber());
            newTeam.setTeamImgUrl(Constant.IMG_URL + headImg(team.getTeamImgUrl()));
            teamList.add(newTeam);
        }
        return teamList;
    }

    // 分割字符串
    private static String headImg(String imgUrl) {
        String[] strings = imgUrl.split(",");
        return strings[0];
    }
}
